﻿using System;
using System.Data;
using System.Data.SqlClient;
using Module.QuanTriHeThong.DanhMuc;

namespace Module.QuanTriHeThong.ThietLapHeThong
{
    public class Kho_Khoa : DBInteractionBase
    {
        public string ID { get; set; }
        public string MaKhoa { get; set; }
        public string MaKho { get; set; }
        public bool IsActive { get; set; }

        public DataTable SelectAll(Kho_Khoa obj, int pageindex, int pagesize, out int recordcount)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMKho_Khoa_SelectAll", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaKhoa", string.IsNullOrEmpty(obj.MaKhoa) ? "" : obj.MaKhoa));
                    dbcmd.Parameters.Add(new SqlParameter("@MaKho", string.IsNullOrEmpty(obj.MaKho) ? "" : obj.MaKho));
                    dbcmd.Parameters.Add(new SqlParameter("@PageIndex", pageindex));
                    dbcmd.Parameters.Add(new SqlParameter("@PageSize", pagesize));
                    dbcmd.Parameters.Add(new SqlParameter("@RecordCount", SqlDbType.Int, 50)
                    {
                        Direction = ParameterDirection.Output
                    });
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    recordcount = Convert.ToInt32(dbcmd.Parameters["@RecordCount"].Value);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("tb_DMKho_Khoa_SelectAll: " + ex.Message);
            }
        }

        public bool Insert(Kho_Khoa obj)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMKho_Khoa_Insert", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaKhoa", obj.MaKhoa));
                    dbcmd.Parameters.Add(new SqlParameter("@MaKho", obj.MaKho));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool DeleteById(string id)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMKho_Khoa_DeleteById", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@ID", id));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}